package com.youfu.crawler;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.xueke.crawler.helper.DownUtil;
import com.xueke.crawler.helper.HttpHelper;
import com.xueke.crawler.helper.HttpUtil;
import org.apache.commons.io.FileUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类说明.
 *
 * @author weibo
 * @since 2018/8/24
 */
public class Main {

    static String rootPath = "http://jiaofu.yousi.com/l-0-0-0-%s/";
    static String domain = "http://jiaofu.yousi.com";

    public static void main(String[] args) {

        //1.logback
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        //获取应用中的所有logger实例
        List<ch.qos.logback.classic.Logger> loggerList = loggerContext.getLoggerList();

        //遍历更改每个logger实例的级别,可以通过http请求传递参数进行动态配置
        for (ch.qos.logback.classic.Logger logger : loggerList) {
            logger.setLevel(Level.toLevel("ERROR"));
        }
        int ct = 0;
        for (int i = 1; i < 45; i++) {
            Document document = HttpHelper.getSimpleDocument(String.format(rootPath, i));
            //System.out.println(document);
            Elements elements = document.select("ul.jf-list-box a");
            System.out.println("page --------------" + i);
            int t = 0;
            for (Element element : elements) {
                if ((t++) % 2 == 0) continue;
                System.out.println(ct + ">>>>>>>>>>>");
                ct++;
                try {

                    String url = domain + element.attr("href");
                    String name = element.text();
                    System.out.println(url + "------------" + name);

                    document = HttpHelper.getSimpleDocument(url);
                    //System.out.println(document);

                    Elements as = document.select(".breads a");
                    String bread = as.stream().map(a -> a.text()).filter(a -> a.length() > 0).collect(Collectors.joining("/"));
                    String address = document.select("#ifrPage").attr("src").replace("/compontent/pdf/?url=", "");
                    // String address = domain + document.select("#ifrPage").attr("src");
                    address = URLDecoder.decode(address);
                    String fileName = address.replace("http://jfpdf.yousi.com/", "");
                    //  System.out.println(bread);
                    // System.out.println(address);
                    // File jiaofu = new File("f:/jiaofu.sql");
                    //  FileUtils.writeStringToFile(jiaofu, String.format("insert into lesson(url,name,download_url,path) values('%s','%s','%s','%s')", url, name, address, bread), true);
                    File dir = new File("f:/" + bread + name);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    File pdf = new File(dir.getAbsolutePath() + "/" + fileName);
                    if (pdf.exists()) {
                        System.out.println("exist -------");
                        continue;
                    }
                    try {
                        HttpUtil.download(address, pdf.getAbsolutePath());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

    }
}
